package com.liuhung.base.util;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class HtmlUtil {



	public static void writerJson(HttpServletResponse response, String jsonStr) {
		writer(response, jsonStr);
	}

	public  static  void writerJson(HttpServletResponse response, Object object) {
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		try {
			response.setContentType("application/json");
			
			writer(response, gson.toJson(object));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static  void writerJson(HttpServletResponse response, Object object,String dateFormat) {
		Gson gson2 = new GsonBuilder().setDateFormat(dateFormat).create();
		try {
			response.setContentType("application/json");
			
			writer(response, gson2.toJson(object));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

	public static void writerHtml(HttpServletResponse response, String htmlStr) {
		writer(response, htmlStr);
	}

	private static void writer(HttpServletResponse response, String str) {
		try {
			// 设置页面不缓存
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = null;
			out = response.getWriter();
			out.print(str);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	

}
